package m.jclecai.com.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import m.jclecai.com.R;
import m.jclecai.com.inter.OnItemClick;

/**
 * Created by hp on 2017/3/31.
 *
 * 公用的玩法弹出框适配器
 */

public class SelectBoxAdapter extends BaseAdapter implements View.OnClickListener {

    private final OnItemClick i;
    private Context context;
    private String[] bonus;
    private String[] mode;
    private int flag;

    public SelectBoxAdapter(Context context , OnItemClick i) {
        this.context = context;
        this.i = i;
    }

    public void setData(String[] mode, String[] bonus) {
        this.mode = mode;
        this.bonus = bonus;
    }

    @Override
    public int getCount() {
        return mode.length;
    }

    @Override
    public Object getItem(int position) {
        return mode[position];
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.select_box_item, null);
            holder.tv_mode = (TextView) convertView.findViewById(R.id.tv_mode);
            holder.tv_bonus = (TextView) convertView.findViewById(R.id.tv_bonus);
            holder.rl_mode = (RelativeLayout) convertView.findViewById(R.id.rl_mode);
            holder.rl_mode.setOnClickListener(this);
            holder.position = position;
            holder.rl_mode.setTag(holder);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_mode.setText(mode[position]);
        holder.tv_bonus.setText(bonus[position]);

        if(position == flag){
            holder.rl_mode.setBackgroundResource(R.drawable.select_box_down);
            holder.tv_mode.setTextColor(holder.tv_mode.getContext().getResources().getColor(R.color.baseRed));
        }else{
            holder.rl_mode.setBackgroundResource(R.drawable.select_box_up);
            holder.tv_mode.setTextColor(holder.tv_mode.getContext().getResources().getColor(R.color.color_666666));
        }
        return convertView;
    }

    //记录点击的position
    @Override
    public void onClick(View v) {
       ViewHolder holder = (ViewHolder)v.getTag();
       flag =  holder.position;


       Log.i("一一一一一一一","flag="+flag);
       i.onItemClick( holder.tv_mode.getText().toString());
       notifyDataSetChanged();
    }

    private class ViewHolder {
        TextView tv_mode;
        TextView tv_bonus;
        RelativeLayout rl_mode;
        int position;
    }
}

















